
/**
 * 匹配图片文件类型
 * @param {*} path 
 * @param {*} imageTypes 
 * @returns 
 */
export const matchFileType = (filepath, imageTypes) => {

    if (!imageTypes) { return false; }

    const arr = filepath.split('.');
    const suffix = arr[arr.length - 1];
    return imageTypes.includes(`.${suffix}`);
}

function genVarByFileName(fileName, type){
    if(!fileName) return '';
    const fileSplit = fileName.split(".")
    let pureFileName = fileSplit.slice(0, fileSplit.length - 1).join(".");
    const joinDot = type === 'css'? '-':'_';
    pureFileName = pureFileName.replace(/[^a-z0-9]/gi, joinDot);
    return type === 'css' ? `--${pureFileName}` : `WEBP_${pureFileName}`.toUpperCase()
}

export function genFileCssVar(fileName){
    return genVarByFileName(fileName, 'css')
}

export function genFileJsVar(fileName){
    return genVarByFileName(fileName, 'js')
}

export function toArray(b){
    return toString.apply(b) === '[object Array]'? b: b ? [b] : [];
}
